import { UIStateEvent } from "R/core/events";
import { Controller } from "../types";
import { OrbitControls } from "./OrbitControls";
import { PointerEventInfo, WheelEventInfo } from "R/core/types";
import { OrthographicCamera, PerspectiveCamera } from "three";


/**
 * 轨道控制器处理
 */
export class OrbitHandler extends Controller {
  orbitControls: OrbitControls

  handler = ({ detail }: UIStateEvent) => {
    const { eventType, pointerType, pointerDownButton, wheelEventDeltaY, whellEventModel, ctrlKey, shiftKey, altKey, metaKey, currentOriginalXOY } = detail
    const pointerInfo: PointerEventInfo = {
      type: eventType,
      pointerType: pointerType!,
      button: pointerDownButton,
      offsetX: currentOriginalXOY.x,
      offsetY: currentOriginalXOY.y,
      pointerId: 0,
      ctrlKey, shiftKey, altKey, metaKey
    }
    switch (eventType) {
      case 'pointerdown':
        this.orbitControls.pointerDownHandler(pointerInfo);
        break;
      case 'pointermove':
        this.orbitControls.pointerMoveHandler(pointerInfo);
        break;
      case 'pointerup':
        this.orbitControls.pointerUpHandler(pointerInfo);
        break;
      case 'wheel':
        const wheelInfo: WheelEventInfo = {
          type: 'wheel',
          deltaY: wheelEventDeltaY,
          ctrlKey, shiftKey, altKey, metaKey,
          offsetX: currentOriginalXOY.x,
          offsetY: currentOriginalXOY.y,
          deltaMode: whellEventModel
        }
        this.orbitControls.mouseWheelHandler(wheelInfo);
        break;

    }

  }

  constructor(camera: PerspectiveCamera | OrthographicCamera, width: number, height: number) {
    super()
    this.orbitControls = new OrbitControls(camera, width, height)
    
  }

}
